﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseUserTest.Controller
{
    public class UserCTRL
    {
        private DBUser dbuser = new DBUser();

        public bool AddUser(string name, string phoneNo, string mail)
        {
            RegexUtilities mailcheck = new RegexUtilities();
            int result;
            if (name.Length > 0 && phoneNo.Length == 8 && int.TryParse(phoneNo, out result) && mailcheck.IsValidEmail(mail))
            {
                
                int phoneNumber = Convert.ToInt32(phoneNo);

                User user = new User();
                user.Name = name;
                user.PhoneNo = phoneNumber;
                user.Mail = mail;

                dbuser.AddUser(user);
                return true;  
            }
            else
            {
                return false;
            }
        }

        public List<User> FindAllUsers()
        {
            return dbuser.FindAllUsers();
        }

        public User FindUser(string phoneNo)
        {
            int result;
            if (phoneNo.Length == 8 && int.TryParse(phoneNo, out result))
            {
                if (dbuser.FindUser(result) != null)
                {
                    return dbuser.FindUser(result);
                }
            }
            return null;

        }

    }
}
